package com.sqfw.project.knowledge.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sqfw.common.enums.knowledge.KlContentOpeTypeEnum;
import com.sqfw.common.utils.DateUtils;
import com.sqfw.common.utils.SecurityUtils;
import com.sqfw.framework.security.LoginUser;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.project.knowledge.domain.KlContentInfo;
import com.sqfw.project.knowledge.domain.cms.CmsKnowledge;
import com.sqfw.project.knowledge.mapper.CmsKnowledgeMapper;
import com.sqfw.project.knowledge.mapper.KlContentInfoMapper;
import com.sqfw.project.knowledge.service.CmsKnowledgeService;
import com.sqfw.project.system.domain.SysDictData;
import com.sqfw.project.system.domain.SysDictType;
import com.sqfw.project.system.mapper.SysDictDataMapper;
import com.sqfw.project.system.mapper.SysDictTypeMapper;
import com.sqfw.project.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * cms同步知识
 *
 * @author tangxt
 * @date 2024-07-01
 * */
@Slf4j
@Service
public class CmsKnowledgeServiceImpl implements CmsKnowledgeService {

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private CmsKnowledgeMapper cmsKnowledgeMapper;

    @Autowired
    public KlContentInfoMapper klContentInfoMapper;

    /** 上海市政府网站集约化平台 apiUrl*/
    @Value("${cmsKnowledge.apiUrl}")
    private  String apiUrl;

    @Value("${cmsKnowledge.siteId}")
    private String siteId;

    private static final String CMS_LOGIN_URL = "/api/auth/vclient/login";

    private static final String CMS_KNOWLEDGE_LIST_URL = "/api/pack/content/page";

    private static final String CMS_KNOWLEDGE_DATA_URL = "/api/pack/content/get";

    private static final String CMS_COLUMN_LIST_URL = "/api/pack/content/channel/tree";

    private static final String PUBLISH_TAG_LIST_URL = "/api/pack/assist/publish-tag/tree";

    // 去字典表查询账号密码
    private static final String CMS_ACCOUNT_PASSWORD = "cms_account_password";

    private static final String CMS_TOKEN_KEY = "token";
    private static final String CMS_TOKEN_DATE_KEY = "token_date";
    private static final Long CMS_TOKEN_TIME = 15 * 60 * 1000L;

    /** 知识库-政策解读-MZ_zhuzhan890 */
    private static final String KL_MZ_ZHUZHAN890 = "kl_mz_zhuzhan890";
    // 知识库-规范性文件-MZ_zhuzhan2739----kl_mz_zhuzhan2739
    private static final String KL_MZ_ZHUZHAN2739 = "kl_mz_zhuzhan2739";
    // 知识库-政策文件-MZ_zhuzhan55--kl_mz_zhuzhan55
    private static final String KL_MZ_ZHUZHAN55 = "kl_mz_zhuzhan55";

    /** cms知识状态：发布状态，0：未发布；1：准备发布；2：正在发布；3：发布成功；4：发布失败 */
    private static final String RELEASE_STATUS = "3";

    private ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();


    /**
     * 登录接口
     * */
    public String cmsLogin() {
        // token 本机缓存
        if (!CollectionUtils.isEmpty(concurrentHashMap)) {
            String token = concurrentHashMap.get(CMS_TOKEN_KEY);
            if (StringUtils.isNotBlank(token)) {
                String tokenDate = concurrentHashMap.get(CMS_TOKEN_DATE_KEY);
                if (StringUtils.isNotBlank(tokenDate)) {
                    Long time =  System.currentTimeMillis() - Long.valueOf(tokenDate);
                    if (CMS_TOKEN_TIME >= time) {
                        return token;
                    }
                }
            }
        }
        Map<String, String> params = new HashMap<>();
        SysDictType sysDictType = sysDictTypeMapper.selectDictTypeByType(CMS_ACCOUNT_PASSWORD);
        if (sysDictType != null) {
            if (StringUtils.isBlank(sysDictType.getRemark())) {
                log.info("cmd-用户名/密码-是空的");
                return null;
            }
            String[] arr = sysDictType.getRemark().split("--");
            params.put("appKey", arr[0]);
            params.put("appSecret", arr[1]);
        }else {
            log.info("cmd-用户名/密码-字典不存在");
            return null;
        }
        String cmsLoginUrl = apiUrl + CMS_LOGIN_URL;
        log.info("登录-cms平台-地址: {}", cmsLoginUrl);
        log.info("登录-cms平台-参数: {}", params.toString());
        String result =  HttpClientUtils.postJson(cmsLoginUrl, params);
        log.info("登录-cms平台-接口返回result: " + result);
        if (StringUtils.isBlank(result)) {
            log.info("登录-cms平台-接口失败");
            return null;
        }else {
            JSONObject resJson = JSONObject.parseObject(result);
            // 成功
            if ("0".equals(resJson.getString("code"))) {
                JSONObject dataJSONObject = resJson.getJSONObject("data");
                String token = dataJSONObject.getString("token");
                concurrentHashMap.put(CMS_TOKEN_KEY, token);
                concurrentHashMap.put(CMS_TOKEN_DATE_KEY, String.valueOf(System.currentTimeMillis()));
                return token;
            }else {
                log.error("登录-cms平台-失败-code：{}, message: {}",
                        resJson.getString("code"), resJson.getString("message"));
                return null;
            }
        }
    }

    /**
     * 定时任务
     * 同步政策解释的知识数据List
     * */
    @Override
    public void syncPolicyInterpretation(String beginDisplayDate, String endDisplayDate) {
        this.queryPolicyInterpretation(beginDisplayDate, endDisplayDate);
    }

    /**
     * 定时任务
     * 政策解释
     * */
    public void queryPolicyInterpretation(String beginDisplayDate, String endDisplayDate) {
        SysDictType sysDictType = sysDictTypeMapper.selectDictTypeByType(KL_MZ_ZHUZHAN890);
        // 备注
        String remark = sysDictType.getRemark();
        String[] arr = remark.split("--");
        this.queryCmsKnowledgeListByChannelId(arr[0], arr[1], beginDisplayDate, endDisplayDate);
    }

    /**
     * 定时任务
     * 同步规范性文件的知识List
     * */
    @Override
    public void syncNormativeDocument(String beginDisplayDate, String endDisplayDate) {
        this.queryNormativeDocument(beginDisplayDate, endDisplayDate);
    }

    /**
     * 定时任务
     * 规范性文件
     * */
    public void queryNormativeDocument(String beginDisplayDate, String endDisplayDate) {
        SysDictType sysDictType = sysDictTypeMapper.selectDictTypeByType(KL_MZ_ZHUZHAN2739);
        // 备注
        String remark = sysDictType.getRemark();
        String[] arr = remark.split("--");
        this.queryCmsKnowledgeListByChannelId(arr[0], arr[1], beginDisplayDate, endDisplayDate);
    }

    /**
     * 定时任务
     * 政策文件: 有许多二级分类和三级分类, 同步知识数据List
     * */
    @Override
    public void syncPolicyDocument(String beginDisplayDate, String endDisplayDate) {
        this.queryPolicyDocument(beginDisplayDate, endDisplayDate);
    }



    /**
     * 定时任务
     * 政策文件: 有许多二级分类和三级分类
     * Policy Document
     * */
    public void queryPolicyDocument(String beginDisplayDate, String endDisplayDate) {
        List<SysDictData> dictDataList = sysDictDataMapper.selectDictDataByType(KL_MZ_ZHUZHAN55);
        for (SysDictData dictData : dictDataList) {
            try {
                // 字典键值
                String dictValue = dictData.getDictValue();
                String[] arr = dictValue.split("--");
                this.queryCmsKnowledgeListByChannelId(arr[0], arr[1], beginDisplayDate, endDisplayDate);
                // 休息20毫秒
                Thread.sleep(20);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 政策文件: 同步知识数据List---递归
     * */
    @Override
    public void syncPolicyDocumentRecursion(String beginDisplayDate, String endDisplayDate) {
        this.queryPolicyDocumentRecursion(beginDisplayDate, endDisplayDate);
    }

    /**
     * 定时任务
     * 政策文件: 有许多二级分类和三级分类----栏目递归
     * */
    public void queryPolicyDocumentRecursion(String beginDisplayDate, String endDisplayDate) {
        SysDictType sysDictType = sysDictTypeMapper.selectDictTypeByType(KL_MZ_ZHUZHAN55);
        // 备注
        String remark = sysDictType.getRemark();
        String[] arr = remark.split("--");
        this.queryCmsKnowledgeListByChannelId(arr[0], arr[1], beginDisplayDate, endDisplayDate);
    }


    /**
     * 根据不同的栏目，查询知识List,
     * @param channelId
     * @param klSortId
     * */
    @Transactional(rollbackFor = Exception.class)
    public void queryCmsKnowledgeListByChannelId(String channelId, String klSortId,
                                                 String beginDisplayDate, String endDisplayDate) {
        Map<String, Object> queryParam = new HashMap<>();
        // 查询参数
        queryParam.put("channelId", channelId);
        queryParam.put("releaseStatus", RELEASE_STATUS);
        // 是否递归栏目
        queryParam.put("recursion", true);
        // 初始数据
        if (StringUtils.isNotBlank(beginDisplayDate) && StringUtils.isNotBlank(endDisplayDate)) {
            queryParam.put("beginDisplayDate", beginDisplayDate);
            queryParam.put("endDisplayDate", endDisplayDate);
        }else {
            // 每天同步一次--同步昨天的
            String date = DateUtils.dateTime(DateUtils.plusDay(new Date(), -1));
            String beginDisplayTime =  date+ " 00:00:00";
            String endDisplayTime = date + " 23:59:59";
            queryParam.put("beginDisplayDate", beginDisplayTime);
            queryParam.put("endDisplayDate", endDisplayTime);
        }
        queryParam.put("orderBy", "display_desc");
        // pageNo
        queryParam.put("pageSize", 1);
        // pageSize
        queryParam.put("pageSize", 500);
        JSONObject jsonObject = this.queryCmsKnowledgeList(queryParam);
        if (jsonObject != null) {
            JSONArray jsonArray = jsonObject.getJSONArray("records");
            log.info(channelId + "-总数量：{}", jsonObject.getString("totalCount"));
            Integer totalPage = jsonObject.getInteger("totalPage");
            // 超过2页了
            if (totalPage >= 2) {
                for (int i = 2; i <= totalPage; i++) {
                    // pageNo
                    queryParam.put("pageSize", i);
                    JSONObject jsonObjectData = this.queryCmsKnowledgeList(queryParam);
                    if (jsonObjectData != null) {
                        JSONArray records = jsonObjectData.getJSONArray("records");
                        if (records != null && records.size() > 0) {
                            jsonArray.addAll(records);
                        }
                    }else {
                        break;
                    }
                }
            }
            if (jsonArray != null && jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObjectData = jsonArray.getJSONObject(i);
                    if (jsonObjectData != null) {
                        String cmsKnowledgeId = jsonObjectData.getString("id");
                        LambdaQueryWrapper<CmsKnowledge> queryWrapper = new LambdaQueryWrapper<>();
                        queryWrapper.eq(CmsKnowledge::getCmsKnowledgeId, cmsKnowledgeId);
                        CmsKnowledge cmsKnowledgeData = cmsKnowledgeMapper.selectOne(queryWrapper);
                        CmsKnowledge cmsKnowledge = new CmsKnowledge();
                        // 不存在，添加； 存在修改
                        if (cmsKnowledgeData == null) {
                            cmsKnowledge.setKlSortId(klSortId);
                            cmsKnowledge.setKlContentType("0");
                            cmsKnowledge.setCmsKnowledgeId(cmsKnowledgeId);
                            cmsKnowledge.setCmsChannelId(channelId);
                            cmsKnowledge.setReleaseStatus(RELEASE_STATUS);
                            cmsKnowledge.setKlContentOpeType("A");
                            cmsKnowledgeMapper.insert(cmsKnowledge);
                        }else {
                            if (cmsKnowledgeData.getIsSync() == 1) {
                                cmsKnowledge.setId(cmsKnowledgeData.getId());
                                cmsKnowledge.setIsSync(0);
                                cmsKnowledge.setKlContentOpeType("M");
                                cmsKnowledge.setUpdateTime(new Date());
                                cmsKnowledgeMapper.updateById(cmsKnowledge);
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * cms稿件列表
     * @param queryParam
     * @return
     * */
    public JSONObject queryCmsKnowledgeList(Map<String, Object> queryParam) {
        String cmsKnowledgeListUrl = apiUrl + CMS_KNOWLEDGE_LIST_URL;
        log.info("查询-cms知识列表-地址: {}", cmsKnowledgeListUrl);
        Map<String, String> headerParam = new HashMap<>();
        String token = cmsLogin();
        if (StringUtils.isBlank(token)) {
            return null;
        }
        headerParam.put("token", token);
        headerParam.put("siteId", siteId);
        log.info("查询-cms知识列表-header: {}", headerParam.toString());
        log.info("查询-cms知识列表-参数: {}", queryParam.toString());
        String result =  HttpClientUtils.postJsonObject(cmsKnowledgeListUrl, headerParam, queryParam, null);
        log.info("查询-cms知识列表-接口返回result: " + result);
        if (StringUtils.isBlank(result)) {
            log.info("查询-cms知识列表-接口失败");
            return null;
        }else {
            JSONObject resJson = JSONObject.parseObject(result);
            // 成功
            if ("0".equals(resJson.getString("code"))) {
                JSONObject jsonObject = resJson.getJSONObject("data");
//                log.info("查询-cms知识列表-JSONObject: {}", jsonObject.toString());
                return jsonObject;
            }else {
                log.error("查询-cms知识列表-失败-code：{}, message: {}",
                        resJson.getString("code"), resJson.getString("message"));
                return null;
            }
        }
    }

    /**
     * 定时任务
     * 同步知识库数据--查询一条，存储一条
     * */
    @Override
    public void syncCmsKnowledge() {
        boolean isSync = true;
        while (isSync) {
            LambdaQueryWrapper<CmsKnowledge> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CmsKnowledge::getIsSync, 0)
                    .last("LIMIT 1000")
                    .orderByDesc(CmsKnowledge::getCreateTime);
            List<CmsKnowledge> cmsKnowledgeList = cmsKnowledgeMapper.selectList(queryWrapper);
            if (!CollectionUtils.isEmpty(cmsKnowledgeList)) {
                for (CmsKnowledge cmsKnowledge : cmsKnowledgeList) {
                    JSONObject jsonObject = this.queryCmsKnowledgeDateById(cmsKnowledge);
                    // 没有数据
                    if (jsonObject == null) {
                        this.syncFail(cmsKnowledge);
                    }else {
                        KlContentInfo klContentInfo = new KlContentInfo();
                        klContentInfo.setKlContentTitle(jsonObject.getString("title"));
                        klContentInfo.setKlContent(jsonObject.getString("txt"));
                        if (StringUtils.isNotBlank(jsonObject.getString("url"))) {
                            cmsKnowledge.setLinkUrl(jsonObject.getString("url"));
                        }
                        // 附件-文件
                        JSONObject attrs = jsonObject.getJSONObject("attrs");
                        if (attrs != null) {
                            if (StringUtils.isNotBlank(attrs.getString("zwPDFUrl"))) {
                                cmsKnowledge.setZwPdfUrl(attrs.getString("zwPDFUrl"));
                            }
                            if (StringUtils.isNotBlank(attrs.getString("zwPDFName"))) {
                                cmsKnowledge.setZwPdfName(attrs.getString("zwPDFName"));
                            }
                        }
                        // 更新知识库
                        if ("M".equals(cmsKnowledge.getKlContentOpeType())) {
                            klContentInfo.setKlId(cmsKnowledge.getKlId());
                            this.syncSuccessUpdate(cmsKnowledge, klContentInfo);
                        }else { // 添加知识库
                            // 关键字
                            if (StringUtils.isNotBlank(jsonObject.getString("author"))) {
                                klContentInfo.setKlContentKeyword(jsonObject.getString("author"));
                            }
                            // 发布日期
                            klContentInfo.setKlContentPublishtime(new Date());
                            klContentInfo.setKlSortId(cmsKnowledge.getKlSortId());
                            klContentInfo.setKlContentType(cmsKnowledge.getKlContentType());
                            this.syncSuccessAdd(cmsKnowledge, klContentInfo);
                        }
                    }
                }
            }else {
                isSync = false;
            }
        }
    }

    /**
     * 定时任务
     * 再次同步失败的知识
     * */
    @Override
    public void syncFailCmsKnowledge() {
        LambdaQueryWrapper<CmsKnowledge> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CmsKnowledge::getIsSync, 2)
                .last("LIMIT 1000")
                .orderByDesc(CmsKnowledge::getCreateTime);
        List<CmsKnowledge> cmsKnowledgeList = cmsKnowledgeMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(cmsKnowledgeList)) {
            for (CmsKnowledge cmsKnowledge : cmsKnowledgeList) {
                JSONObject jsonObject = this.queryCmsKnowledgeDateById(cmsKnowledge);
                // 没有数据
                if (jsonObject == null) {
                    this.syncFail(cmsKnowledge);
                }else {
                    KlContentInfo klContentInfo = new KlContentInfo();
                    klContentInfo.setKlContentTitle(jsonObject.getString("title"));
                    klContentInfo.setKlContent(jsonObject.getString("txt"));
                    if (StringUtils.isNotBlank(jsonObject.getString("url"))) {
                        cmsKnowledge.setLinkUrl(jsonObject.getString("url"));
                    }
                    // 附件
                    JSONObject attrs = jsonObject.getJSONObject("attrs");
                    if (attrs != null) {
                        if (StringUtils.isNotBlank(attrs.getString("zwPDFUrl"))) {
                            cmsKnowledge.setZwPdfUrl(attrs.getString("zwPDFUrl"));
                        }
                        if (StringUtils.isNotBlank(attrs.getString("zwPDFName"))) {
                            cmsKnowledge.setZwPdfName(attrs.getString("zwPDFName"));
                        }else {
                            cmsKnowledge.setZwPdfName("PDF");
                        }
                    }
                    // 更新
                    if ("M".equals(cmsKnowledge.getKlContentOpeType())) {
                        klContentInfo.setKlId(cmsKnowledge.getKlId());
                        this.syncSuccessUpdate(cmsKnowledge, klContentInfo);
                    }else { // 添加
                        // 关键字
                        if (StringUtils.isNotBlank(jsonObject.getString("author"))) {
                            klContentInfo.setKlContentKeyword(jsonObject.getString("author"));
                        }
                        // 发布日期
                        klContentInfo.setKlContentPublishtime(new Date());
                        klContentInfo.setKlSortId(cmsKnowledge.getKlSortId());
                        klContentInfo.setKlContentType(cmsKnowledge.getKlContentType());
                        this.syncSuccessAdd(cmsKnowledge, klContentInfo);
                    }
                }
            }
        }else {
            log.info("本次没有失败的知识List");
        }
    }


    /**
     * 同步失败
     * @param cmsKnowledge
     * */
    @Transactional(rollbackFor = Exception.class)
    public void syncFail(CmsKnowledge cmsKnowledge) {
        cmsKnowledge.setIsSync(2);
        cmsKnowledge.setUpdateTime(new Date());
        cmsKnowledge.setSyncNumber(cmsKnowledge.getSyncNumber() + 1);
        cmsKnowledgeMapper.updateById(cmsKnowledge);
    }

    /**
     * 同步成功,添加
     * @param cmsKnowledge
     * @param klContentInfo
     * */
    @Transactional(rollbackFor = Exception.class)
    public void syncSuccessAdd(CmsKnowledge cmsKnowledge, KlContentInfo klContentInfo) {
        cmsKnowledge.setIsSync(1);
        cmsKnowledge.setUpdateTime(new Date());
        cmsKnowledge.setSyncNumber(cmsKnowledge.getSyncNumber() + 1);
        String klid  = "SQ" + klContentInfoMapper.getKlId();
        cmsKnowledge.setKlId(klid);
        cmsKnowledgeMapper.updateById(cmsKnowledge);
        klContentInfo.setKlId(klid);
        this.addKlContentInfo(klContentInfo);
    }

    /**
     * 同步成功,修改知识
     * @param cmsKnowledge
     * @param klContentInfo
     * */
    @Transactional(rollbackFor = Exception.class)
    public void syncSuccessUpdate(CmsKnowledge cmsKnowledge, KlContentInfo klContentInfo) {
        cmsKnowledge.setIsSync(1);
        cmsKnowledge.setUpdateTime(new Date());
        cmsKnowledge.setSyncNumber(cmsKnowledge.getSyncNumber() + 1);
        cmsKnowledgeMapper.updateById(cmsKnowledge);
        klContentInfo.setKlContentOpeType("M");
        // 修改知识数据，根据klid修改
        int i = klContentInfoMapper.updateKlContentInfo(klContentInfo);

    }


    /**
     * 添加知识数据; 是否案例(1-是；0-不是)
     * @param klContentInfo
     * @return
     * */
    @Transactional(rollbackFor = Exception.class)
    public int addKlContentInfo(KlContentInfo klContentInfo) {
        // 一些默认值
        // 紧急程度0：一般；1：紧急
        klContentInfo.setKlContentLevel("0");
        // 是否重要--是否热点 （1是0否）
        klContentInfo.setKlContentIshot("0");
        // 6 待审核, 1 审核通过, -2 审核未通过, 2 删除待审核
        klContentInfo.setKlContentStatus("6");
        // 创建人
        klContentInfo.setKlContentCreater("326");
        // 创建人姓名(真实姓名)
        klContentInfo.setKlContentCreaterName("知识库管理员");
        // 来源:所属组织机构(创建人部门id)
        klContentInfo.setKlContentSources("281");
        // 创建时间
        klContentInfo.setKlContentCreatetime(new Date());
        // 修改时间
        klContentInfo.setKlContentUpdatetime(new Date());
        // 来源方式:1.界面录入2.数据接口3.webservice接口 4.导入文件
        klContentInfo.setKlContentSourcesType("2");
        // 是否上报给12345（0否，1是）
        klContentInfo.setKlContentIshotline("0");
        // 是否置顶（0否，1是）
        klContentInfo.setKlContentIstop("0");
        // 操作类型(知识类型) A-新增 M-修改 D-删除 C-核实
        klContentInfo.setKlContentOpeType(KlContentOpeTypeEnum.A.getKey());
        // 是否删除 0 正常 1删除
        klContentInfo.setKlContentIsDel("0");
        // 核实时间--新增时，该时间就是分布时间
        if (StringUtils.isBlank(klContentInfo.getKlUpdateCycle())) {
            klContentInfo.setKlUpdateCycle("365");
        }
        int day = Integer.parseInt(klContentInfo.getKlUpdateCycle()) - 7;
        klContentInfo.setKlStartVerifyTime(DateUtils.plusDay(klContentInfo.getKlContentPublishtime(), day));
        // 点击量
        klContentInfo.setKlContentHits(0L);
        // 评分
        klContentInfo.setKlContentScore(0.0);
        // 有效期
        klContentInfo.setKlContentValiditytime(DateUtils.parseDate("2099-12-30 23:59:59"));
        // 知识来源名称(知识来源)
        klContentInfo.setKlContentBelongUnit("上海民政网");
        // 是否案例(1-是；0-不是)
        klContentInfo.setIsCase("0");
        return klContentInfoMapper.insertKlContentInfo(klContentInfo);
    }


    /**
     * cms稿件详情
     * @param cmsKnowledge cms知识id
     * */
    public JSONObject queryCmsKnowledgeDateById(CmsKnowledge cmsKnowledge) {
        String cmsKnowledgeDateUrl = apiUrl + CMS_KNOWLEDGE_DATA_URL;
        log.info("查询-cms知识详情-地址: {}", cmsKnowledgeDateUrl);
        Map<String, String> headerParam = new HashMap<>();
        String token = cmsLogin();
        if (StringUtils.isBlank(token)) {
            return null;
        }
        headerParam.put("token", token);
        headerParam.put("siteId", siteId);
        log.info("查询-cms知识详情-header: {}", headerParam.toString());
        Map<String, String> queryParam = new HashMap<>();
        // 查询参数
        queryParam.put("id", cmsKnowledge.getCmsKnowledgeId());
        log.info("查询-cms知识详情-参数: {}", queryParam.toString());
        String result =  HttpClientUtils.postJson(cmsKnowledgeDateUrl, headerParam, queryParam);
        log.info("查询-cms知识详情-接口返回result: " + result);
        if (StringUtils.isBlank(result)) {
            log.info("查询-cms知识详情-接口失败");
            cmsKnowledge.setRemark("查询-cms知识详情-接口失败");
            return null;
        }else {
            JSONObject resJson = JSONObject.parseObject(result);
            // 成功
            if ("0".equals(resJson.getString("code"))) {
                JSONObject dataJSONObject = resJson.getJSONObject("data");
//                log.info("查询-cms知识详情-dataJSONObject: {}", dataJSONObject.toString());
                return dataJSONObject;
            }else {
                log.error("查询-cms知识详情-失败-code：{}, message: {}",
                        resJson.getString("code"), resJson.getString("message"));
                String remark = resJson.getString("message");
                if (remark.length() > 3000) {
                    cmsKnowledge.setRemark(remark.substring(0,3000));
                }else {
                    cmsKnowledge.setRemark(remark);
                }
                return null;
            }
        }

    }

    /**
     * 栏目树---没有使用
     *
     * */
    public void queryCmsColumnList() {

        String cmsColumnListUrl = apiUrl + CMS_COLUMN_LIST_URL;
        log.info("查询-cms知识栏目树-地址: {}", cmsColumnListUrl);
        Map<String, String> headerParam = new HashMap<>();
        String token = cmsLogin();
        if (StringUtils.isBlank(token)) {
            return;
        }
        headerParam.put("token", token);
        headerParam.put("siteId", siteId);
        log.info("查询-cms知识栏目树-header: {}", headerParam.toString());
        Map<String, String> queryParam = new HashMap<>();
        // 查询参数

        log.info("查询-cms知识栏目树-参数: {}", queryParam.toString());
        String result =  HttpClientUtils.postJson(cmsColumnListUrl, headerParam, queryParam);
//        String result = "{\"code\":0,\"message\":\"string\",\"data\":[{\"id\":\"12345\",\"description\":\"string\",\"displayFlag\":\"0\",\"hasChild\":true,\"keywords\":\"string\",\"link\":\"string\",\"modelId\":\"string\",\"name\":\"string\",\"namePath\":\"string\",\"pageSize\":0,\"parentId\":\"string\",\"path\":\"string\",\"priority\":0,\"remarks\":\"string\",\"siteId\":\"string\",\"title\":\"string\",\"titleImageHeight\":0,\"titleImageUrl\":\"string\",\"titleImageWidth\":0,\"totalPage\":0,\"url\":\"string\",\"workflowId\":\"string\",\"afterCheckFlag\":\"1\",\"createDate\":\"2020-03-30 00:00:00\",\"createUser\":{\"id\":\"string\"},\"updateDate\":\"2020-03-30 00:00:00\",\"updateUser\":{\"id\":\"string\"}},{\"id\":\"67890\",\"description\":\"string\",\"displayFlag\":\"0\",\"hasChild\":true,\"keywords\":\"string\",\"link\":\"string\",\"modelId\":\"string\",\"name\":\"string\",\"namePath\":\"string\",\"pageSize\":0,\"parentId\":\"string\",\"path\":\"string\",\"priority\":0,\"remarks\":\"string\",\"siteId\":\"string\",\"title\":\"string\",\"titleImageHeight\":0,\"titleImageUrl\":\"string\",\"titleImageWidth\":0,\"totalPage\":0,\"url\":\"string\",\"workflowId\":\"string\",\"afterCheckFlag\":\"1\",\"createDate\":\"2020-03-30 00:00:00\",\"createUser\":{\"id\":\"string\"},\"updateDate\":\"2020-03-30 00:00:00\",\"updateUser\":{\"id\":\"string\"}}]}";
        log.info("查询-cms知识栏目树-接口返回result: " + result);
        if (StringUtils.isBlank(result)) {
            log.info("查询-cms知识栏目树-接口失败");
            return ;
        }else {
            JSONObject resJson = JSONObject.parseObject(result);
            // 成功
            if ("0".equals(resJson.getString("code"))) {
                JSONArray jsonArray = resJson.getJSONArray("data");
                log.info("查询-cms知识栏目树-jsonArray: {}", jsonArray.toString());

            }else {
                log.error("查询-cms知识栏目树-失败-code：{}, message: {}",
                        resJson.getString("code"), resJson.getString("message"));
                return ;
            }
        }
    }

    /**
     * 获取集成发布标签---没有使用
     *
     */
    public void queryPublishTagList() {
        String publishTagListUrl = apiUrl + PUBLISH_TAG_LIST_URL;
        log.info("获取集成发布标签-地址: {}", publishTagListUrl);
        Map<String, String> headerParam = new HashMap<>();
        String token = cmsLogin();
        if (StringUtils.isBlank(token)) {
            return;
        }
        headerParam.put("token", token);
        headerParam.put("siteId", siteId);
        log.info("获取集成发布标签-header: {}", headerParam.toString());
        Map<String, String> queryParam = new HashMap<>();
        // 查询参数

        log.info("获取集成发布标签-参数: {}", queryParam.toString());
        String result =  HttpClientUtils.postJson(publishTagListUrl, headerParam, queryParam);
        log.info("获取集成发布标签-接口返回result: " + result);
        if (StringUtils.isBlank(result)) {
            log.info("获取集成发布标签-接口失败");
            return ;
        }else {
            JSONObject resJson = JSONObject.parseObject(result);
            // 成功
            if ("0".equals(resJson.getString("code"))) {
                JSONArray jsonArray = resJson.getJSONArray("data");
                log.info("获取集成发布标签-jsonArray: {}", jsonArray.toString());

            }else {
                log.error("获取集成发布标签-失败-code：{}, message: {}",
                        resJson.getString("code"), resJson.getString("message"));
                return ;
            }
        }
    }






}
